<?php

App::uses('CrudController', 'Controller');

class QuestionsController extends CrudController
{
    public $_model = 'Question';

    public function admin() {
        $args = func_get_args();
        $n = $args[0];

        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $appBosch->src_id = $args[$n];
        $appBosch->src_type = $n;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Evaluación de certificación'));
        $this->set('description', $this->getDescription($args));
    }

    private function getDescription($args) {
        $cad = '';

        if ($args[0] >= 1) {
            $this->loadModel('Moe');
            $this->Moe->id = $args[1];
            $moe = $this->Moe->read();
            $cad .= $moe['Moe']['name'] . ' - ';
        }
        if ($args[0] >= 2) {
            $this->loadModel('Area');
            $this->Area->id = $args[2];
            $moe = $this->Area->read();
            $cad .= $moe['Area']['name'] . ' - ';
        }
        if ($args[0] >= 3) {
            $this->loadModel('Line');
            $this->Line->id = $args[3];
            $moe = $this->Line->read();
            $cad .= $moe['Line']['name'] . ' - ';
        }
        if ($args[0] >= 4) {
            $this->loadModel('Workstation');
            $this->Workstation->id = $args[4];
            $moe = $this->Workstation->read();
            $cad .= $moe['Workstation']['description'] . ' - ';
        }
        $cad = rtrim($cad, ' - ');
        return mb_strtoupper($cad);
    }

    public function uploadFiles($type) {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'files';
        $file = array();
        $name = '';
        if (!file_exists($path)) {
            mkdir ($path, 0777, true);
            $sub_path = $path . DS . 'images';
            mkdir ($sub_path, 0777, true);
            $sub_path = $path . DS . 'videos';
            mkdir ($sub_path, 0777, true);
        }
        switch($type) {
            case 1: $path .= DS . 'images';
                $name = 'file.img';
                $file = $_FILES['img'];
                break;
            case 2: $path .= DS . 'videos';
                $name = 'file.vid';
                $file = $_FILES['vid'];
                break;
        }
        move_uploaded_file($file['tmp_name'], $path . DS . $file['name']);
        $this->Session->write($name . '.name', $file['name']);
        $this->Session->write($name . '.path', $path);
        $succes = '';
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }

    public function deletedUploaded() {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'files';
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            unlink($path . DS . 'images' . DS . $file['name']);
            $this->Session->delete("file.img");
        }
        if($this->Session->check('file.vid')) {
            $file = $this->Session->read('file.vid');
            unlink($path . DS . 'videos' . DS . $file['name']);
            $this->Session->delete("file.vid");
        }
        $succes = '';
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($params) {
        $params = json_decode($params);
        $m = $this->_model;
        $records = $this->$m->getEnabledBySource($params);
        for ($i = 0; $i < count($records); $i++ ) {
            $records[$i]['mandatory'] = $records[$i]['mandatory'] == '1' ? true : false;
            $records[$i]['required'] = $records[$i]['required'] == '1' ? true : false;
        }
        return $records;
    }

    protected function afterCreate(&$newRecord) {
        $m = $this->_model;
        $this->$m->id = $newRecord['id'];
        $files = array();
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            $ext = explode('.', $file['name']);
            $newName = $file['path'] . DS . $newRecord['id'] . '.' . $ext[1];
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/images/' . $newRecord['id'] . '.' . $ext[1];
            $files['image'] = $route;
            $newRecord['img'] = $route;
            $this->Session->delete("file.img");
        }
        if($this->Session->check('file.vid')) {
            $file = $this->Session->read('file.vid');
            $ext = explode('.', $file['name']);
            $newName = $file['path'] . DS . $newRecord['id'] . '.' . $ext[1];
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/videos/' . $newRecord['id'] . '.' . $ext[1];
            $files['video'] = $route;
            $newRecord['vid'] = $route;
            $this->Session->delete("file.vid");
        }
        $this->$m->save($files);
        return true;
    }

    protected function afterUpdate($id, &$updatedRecord) {
        $m = $this->_model;
        $this->$m->id = $id;
        $reg = $this->$m->read();
        $files = array();
        if($this->Session->check('file.img')) {
            if ($reg['Question']['image'] != null) {
                unlink(WWW_ROOT . str_replace(Router::url('/', true), '', $reg['Question']['image']));
            }
            $file = $this->Session->read('file.img');
            $ext = explode('.', $file['name']);
            $newName = $file['path'] . DS . $id . '.' . $ext[1];
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/images/' . $id . '.' . $ext[1];
            $files['image'] = $route;
            $updatedRecord['img'] = $route;
            $this->Session->delete("file.img");
        }
        if($this->Session->check('file.vid')) {
            if ($reg['Question']['video'] != null) {
                unlink(WWW_ROOT . str_replace(Router::url('/', true), '', $reg['Question']['video']));
            }
            $file = $this->Session->read('file.vid');
            $ext = explode('.', $file['name']);
            $newName = $file['path'] . DS . $id . '.' . $ext[1];
            rename($file['path'] . DS . $file['name'], $newName);
            $route = Router::url('/', true) . 'PHP_TEMP/files/videos/' . $id . '.' . $ext[1];
            $files['video'] = $route;
            $updatedRecord['vid'] = $route;
            $this->Session->delete("file.vid");
        }
        $this->$m->save($files);
        return true;
    }

    protected function c($model) {
        return array(
            'text' => $model->text,
            'description' => $model->text,
            'source_id' => $model->src_id,
            'type' => $model->src_type,
            'img' => $model->img,
            'vid' => $model->vid,
            'mandatory' => $model->mandatory === true ? Question::MANDATORY : Question::MANDATORY_OFF,
            'required' => $model->required === true ? Question::REQUIRED_ON : Question::REQUIRED_OFF,
            'admissible_answers' => $model->admissible_answers,
        );
    }

    protected function u($model) {
        return array(
            'text' => $model->text,
            'description' => $model->text,
            'img' => $model->img,
            'vid' => $model->vid,
            'mandatory' => $model->mandatory === true ? Question::MANDATORY : Question::MANDATORY_OFF,
            'required' => $model->required === true ? Question::REQUIRED_ON : Question::REQUIRED_OFF,
            'admissible_answers' => $model->admissible_answers,
        );
    }

    protected function id($model) {
        return $model->value;
    }
}